const { app } = require('electron');
const path = require('node:path');
const { exec}  = require('child_process');
const winston = require('winston');
const printer = require('pdf-to-printer');
const log = require('electron-log');


const logger = winston.createLogger({
  transports: [
    new winston.transports.Console({
      format: winston.format.simple(),
    }),
  ],
});

function printPdf(){
  const appPath = app.isPackaged ? path.dirname(app.getPath('exe')) : app.getAppPath();
  const filePath = path.join(appPath, '/pdf', `22.pdf`);
  const printerName = 'Fax'; // 替换为你的打印机名称
  printer.getPrinters().then(console.log);

printer.print(filePath, {silent: false, printDialog: true})
  .then(() => {
    log.info('打印成功');
  })
  .catch(err => {
    log.info('打印失败', err);
  });


//   exec('wmic printer get name', (error, stdout, stderr) => {
//     if (error) {
//       console.error(`获取打印机失败: ${error.message}`);
//       return;
//     }
//     const printers = stdout.split('\n').filter(line => line.trim() !== '');
//     console.log('useable printers :', printers);
//   });

//   exec(`print /d: "${printerName}" "${pdfPath}"`, (error,stdout,stderr) => {
//     console.log("🚀 ~ exec ~ stderr:", Buffer.from(stderr, 'utf-8').toString('utf8'))
//     logger.info('你好，世界！')
//     console.log("🚀 ~ exec ~ stdout:", Buffer.from(stdout, 'utf-8').toString('utf-8'))
//     // console.log("🚀 ~ exec ~ error:", Buffer.from(error, 'utf-8').toString())
//     if (error) {

//       console.error(`print error: ${error.message}`);
//       return;
//     }
//     console.log('print success');
//   });
}

module.exports = {
    printPdf
}